package CtrLayer;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import DBLayer.DBApartment;
import DBLayer.DBBeneficiaryPayment;
import DBLayer.IFDBBeneficiaryPayment;
import ModelLayer.Apartment;
import ModelLayer.BeneficiaryPayment;

public class CtrBeneficiaryPayment {
	
	private IFDBBeneficiaryPayment dbBenPay;
	
	public CtrBeneficiaryPayment(){
		dbBenPay = DBBeneficiaryPayment.getInstance();
	}
	
	public int insert(Timestamp date, double amount, String type){
		return dbBenPay.insert(new BeneficiaryPayment(-1,date,amount,type));
	}
	
	public int update(int paymentId,Timestamp date, double amount, String type){
		return dbBenPay.update(new BeneficiaryPayment(paymentId,date,amount,type));
	}
	
	public int delete(int paymentId){
		return dbBenPay.delete(paymentId);
	}
	
	public ArrayList<BeneficiaryPayment> getAll(){
		return dbBenPay.getAll();
	}
	
	public BeneficiaryPayment getBeneficiaryPayment(int paymentId){
		return dbBenPay.getBeneficiaryPayment(paymentId);
	}
	
	public ArrayList<BeneficiaryPayment> getAll_where(String wClause){
		return dbBenPay.getAll_where(wClause);
		
	}
	
	public ArrayList<BeneficiaryPayment> searchRandom(String searchF){
		return dbBenPay.searchRandom(searchF);
	}
	
	
	
	public boolean payBeneficiariesThisMonth()
	{
		Timestamp date = new Timestamp(new Date().getTime());
		
		if(dbBenPay.alreadyPaid(date)) return false;
		
		
		ArrayList<Apartment> apartments;
		BeneficiaryPayment benPayment = new BeneficiaryPayment();
		benPayment.setDate(date);
		
		
		apartments= DBApartment.getInstance().getAll_withAutoLevy();
		
		for(Apartment apart : apartments)
		{
			benPayment.setApartment(apart);
			
			benPayment.setAmount(apart.getLevyAmount());
			benPayment.setType("levy");
			dbBenPay.insert(benPayment);
		}

		
		apartments= DBApartment.getInstance().getAll_withAutoRate();
		
		for(Apartment apart : apartments)
		{
			benPayment.setApartment(apart);
			benPayment.setAmount(apart.getRate());
			benPayment.setType("rate");
			dbBenPay.insert(benPayment);
		}
		
		return true;
	}
}
